package com.mm;

import com.mm.util.common.IpUtil;
import com.thebeastshop.forest.springboot.annotation.ForestScan;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;
import java.net.InetAddress;

/**
 * @author yl
 */
@Slf4j
@ForestScan(basePackages = "com.mm.base.forest")
@ServletComponentScan(value = "com.mm.base.filter")
@SpringBootApplication
public class MuMuApplication {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext application = SpringApplication.run(MuMuApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = IpUtil.getLocalIp();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path");
        log.info("\n----------------------------------------------------------\n\t" +
                "Application spring-mumu-core is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:" + port + path + "\n\t" +
                "External: \thttp://" + ip + ":" + port + path + "\n\t" +
                "External: \thttp://" + ip + ":" + port + path + "koTime\n\t" +
                "Swagger Api: \thttp://" + ip + ":" + port + path + "doc.html\n\t" +
                "\n----------------------------------------------------------");
    }

    /**
     * 配置上传文件大小的配置
     *
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        // KB 或者 MB 都可以 1MB=1024KB。1KB=1024B(字节)
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //  设置单个文件大小
        factory.setMaxFileSize(DataSize.ofMegabytes(100));
        /// 设置总上传文件大小
        factory.setMaxRequestSize(DataSize.ofMegabytes(100));
        return factory.createMultipartConfig();
    }

}
